#!/bin/sh

#if grep 'add by Kloxo-ST' /etc/hosts >/dev/null 2>&1 ; then
if [ "$(grep 'add by Kloxo-ST' /etc/hosts)" != "" ] ; then
	echo "- 'add by Kloxo-ST' already exists in /etc/hosts"
	exit
fi

sed '/### begin/,/### end/d' /etc/hosts > /etc/hosts2

hs=$(hostname -s)
## use it instead 'hostname -f'
hf=$(hostname)
#ip=$(ifconfig | grep -i 'inet addr:' | grep -v '127.0.0.1' | awk '{print $2}' | sed 's/addr\://')
ip=$(hostname -i | awk '{print $1}')

echo "- add parameters between 'add by Kloxo-ST' in /etc/hosts"
echo "" >> /etc/hosts2
echo "### begin - add by Kloxo-ST" >> /etc/hosts2
echo "${ip} ${hf} ${hs}" >> /etc/hosts2
echo "### end - add by Kloxo-ST" >> /etc/hosts2

mv -f /etc/hosts2 /etc/hosts

## ST -- trick for temporary change hostname
sysctl kernel.hostname=$hf >/dev/null 2>&1
